/*************************************************************************
  > File Name: client.c
  > Author: QiyangXia
  > Mail: 834061456@qq.com 
  > Created Time: 2022年02月17日 星期四 18时36分34秒
 ************************************************************************/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

static char user_name[20]={0};
static char user_pwd[20]={0};
static char buff[256]={0};
int pick_num;

int reg(int socket);
int login(int sockfd);
int client_main_loop(int sockfd);
int create_tcp_client_socket(char *ipaddr,unsigned short port);
int main(int argc,char *argv[])
{

	int sockfd = -1;
	int iport = 0;
	if(argc < 3)
	{
		printf("The argument is too few\n");
		return 1;
	}

	sscanf(argv[2],"%d",&iport);
	if(iport < 0 || iport > 0xFFFF)
	{
		printf("port is invalid\n");
		return 2;
	}

	sockfd = create_tcp_client_socket(argv[1],(unsigned short)iport);
	if(sockfd < 0)
	{
		printf("create_tcp_client_socket failed\n");
		return 3;
	}

	client_main_loop(sockfd);

	close(sockfd);
	sockfd = -1;
	return 0;
}


int create_tcp_client_socket(char *ipaddr,unsigned short port)
{
	int sockfd = -1;
	int ret = 0;
	struct sockaddr_in servaddr;

	sockfd = socket(AF_INET,SOCK_STREAM,0);
	if(sockfd < 0)
	{
		printf("call socket function failed\n");
		return -1;
	}

	bzero(&servaddr,sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(port);
	inet_aton(ipaddr,&servaddr.sin_addr);

	ret = connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
	if(ret < 0)
	{
		close(sockfd);
		sockfd = -1;
		printf("connect server failed\n");
		return ret;
	}

	return sockfd;
}

char *mygets(char *pout,int size);
int client_main_loop(int sockfd)
{

	int flag=0;
	printf("............................连接服务器成功!.........................\n");   
	printf("...............................Welcome..............................\n");
	printf("............................ register: 1 ...........................\n");
	printf("............................ logon:    2 ...........................\n");
	printf("............................ quit:     3 ...........................\n");

	while(1)
	{
		char choose[10]={0};
		mygets(choose,sizeof(choose));
		//printf("%s\n",choose);
		//发送指令给服务器需要注册还是登录
		if(0==strcmp(choose,"1") || 0==strcmp(choose,"2"))
		{
			pick_num=write(sockfd,choose,strlen(choose));

			if(read(sockfd,buff,sizeof(buff))>0)
			{
				;
			}
		}

		if(0==strcmp(choose,"1"))
		{//用户注册
			printf("------------------register--------------------\n");
			if(0!=reg(sockfd))
			{
				close(sockfd);
				return 0;
			}
			printf("register success!!!\n");
			continue;
		}
		if(0==strcmp(choose,"2"))
		{//用户登录
			flag=0;
			printf("------------------login--------------------\n");
			if(0!=login(sockfd))
			{
				close(sockfd);
				sockfd = -1;
				return 0;
			}else{
				flag=1;
				break;
			}
		}
		if(0==strcmp(choose,"3"))
		{
			printf("byebye!!\n");
			close(sockfd);
			sockfd = -1;
			return 0;
		}else{
			printf("we only can deal with 1,2,3\n");
			continue;
		}
	}

//	time_t t;
//	char timebuf[50]="00-00-00-0000-00-00";
	while(flag==1)
	{
		printf("--------------welcome to daydayUPUP--------------------\n");
		printf("Plze enter the word you want search: (#exit for exit  #hist for histroy)\n");
		bzero(buff,sizeof(buff));
		mygets(buff,sizeof(buff));

		if(strcmp(buff,"#exit")==0)
		{
			printf("byebye!\n");
			close(sockfd);
			sockfd = -1;
			return 0;
		}else if(strcmp(buff,"#hist")==0)
		{
	//		time(&t);
	//		ctime_r(&t,timebuf);
	//		printf("%s\n",timebuf);
			pick_num=write(sockfd,buff,strlen(buff));
			pick_num=read(sockfd,buff,512);

			printf("%s\n",buff);
			continue;
		}
		pick_num=write(sockfd,buff,strlen(buff));
		if(0>pick_num)
		{
			perror("write");
			break;
		}

		memset(buff,0,sizeof(buff));
		if(read(sockfd,buff,sizeof(buff))>0)
		{
			printf("##################################################\n");
			printf("%s\n",buff);
			printf("##################################################\n");
		}
		else
		{
			break;
		}

	}
	return 0;
}

char *mygets(char *pout,int size)
{
	char *p = NULL;
	fgets(pout,size,stdin);
	p = pout + strlen(pout) - 1;
	if(*p == '\n')
	{
		*p = '\0';
	}else{
		while(getchar() != '\n'){}
	}
	return pout;
}

int reg(int sockfd)
{
	while(1)
	{
		printf("Plz enter user name:\n");
		memset(user_name,0,sizeof(user_name));
		mygets(user_name,sizeof(user_name));
		printf("Plz enter the PWD:\n");
		memset(user_pwd,0,sizeof(user_pwd));
		mygets(user_pwd,sizeof(user_pwd));
		if(strlen(user_pwd)==0 || strlen(user_name)==0 )
		{
			printf("id&pwd cannot be bzero,PLZ enter again\n");
			continue;
		}
		pick_num=write(sockfd,user_name,strlen(user_name));

		if(read(sockfd,buff,sizeof(buff))>0)
		{
			;
		}

		pick_num=write(sockfd,user_pwd,strlen(user_pwd));
		break;
	}
	return 0;
}

int login(int sockfd)
{
	while(1)
	{
		printf("Plz enter user name:\n");
		memset(user_name,0,sizeof(user_name));
		mygets(user_name,sizeof(user_name));
		printf("Plz enter the PWD:\n");
		memset(user_pwd,0,sizeof(user_pwd));
		mygets(user_pwd,sizeof(user_pwd));
		if(strlen(user_pwd)==0 || strlen(user_name)==0 )
		{
			printf("id&pwd cannot be bzero,PLZ enter again\n");
			continue;
		}
		break;
	}
	pick_num=write(sockfd,user_name,strlen(user_name));

	if(read(sockfd,buff,sizeof(buff))>0)
	{
		;
	}

	pick_num=write(sockfd,user_pwd,strlen(user_pwd));

	memset(buff,0,sizeof(buff));
	read(sockfd,buff,sizeof(buff));
	printf("%s\n",buff);
	if(0==strcmp(buff,"login fail!!"))
	{
		return -1;
	}
	return 0;
}
